package com.candoo.oil.scan;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

public class OrderListActivity extends AppCompatActivity {


    private final static String SCAN_ACTION = "scan.rcv.message";

    private String barcodeStr;
    private EditText showScanResult;

    private SeekBar _sbar;



    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            byte[] barocode = intent.getByteArrayExtra("barocode");
            int barocodelen = intent.getIntExtra("length", 0);
            byte temp = intent.getByteExtra("barcodeType", (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            barcodeStr = new String(barocode, 0, barocodelen);
           // showScanResult.append("广播输出：");
           // showScanResult.append(barcodeStr);
           // showScanResult.append("\n");
           showScanResult.setText(barcodeStr);
            click_search(null);
           Config.sm.stopScan();
        }

    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        Log.v("info","单号处理界面创建");
        _init();

    }

    @Override
    protected void onPause() {
       //暂停
        super.onPause();
        Config.sm.stopScan();
        unregisterReceiver(mScanReceiver);
        Log.e("info","单号处理界面onPause");
        //if(sm != null) {
        //    sm.stopScan();
       // }
       // unregisterReceiver(mScanReceiver);
    }
    @Override
    protected void onResume() {
        //恢复
        super.onResume();
        TextView _account=(TextView) findViewById(R.id.account);
        _account.setText("账号:"+Config.Account);
        Log.e("Config.isLogin",Config.isLogin?"true":"false");
        Log.e("Config.isLogining",Config.isLogining?"true":"false");
        if(!Config.isLogin&&!Config.isLogining)
        {//未登录
           // IntentFilter filter = new IntentFilter();
           // filter.addAction(Config.EXIT_ACTION);
           // this.registerReceiver(this.broadcastReceiver, filter);
            //Intent intent = new Intent();
            //intent.setAction(GlobalVarable.EXIT_ACTION);// 退出动作
            //this.sendBroadcast(intent);// 发送广播
           // super.finish(); //退出后台线程,以及销毁静态变量
            System.exit(0);
            //finish();
           // System.exit(0);//正常退出App
           // android.os.Process.killProcess(android.os.Process.myPid());
            //return;
        }
        else
        {
            IntentFilter filter = new IntentFilter();
            filter.addAction(SCAN_ACTION);
            registerReceiver(mScanReceiver, filter);
           // ScanClass.Start();

        }



        Log.e("info","单号处理界面onResume");
      // IntentFilter filter = new IntentFilter();
      //  filter.addAction(SCAN_ACTION);
      //  registerReceiver(mScanReceiver, filter);
    }

    public void openDetail(View view)
    {
        Intent intent = new Intent(this, OrderDetailActivity.class);
        startActivity(intent);
    }

    public void click_search(View view)
    {

        Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
        intent.putExtra("oid", showScanResult.getText().toString());
        OrderListActivity.this.startActivity(intent);

    }

    public void openLogin(View view)
    {
        Config.isLogin=false;
        Config.isLogining=true;
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    private  void _init()
    {//初始化功能
        _sbar=(SeekBar) findViewById(R.id.seekBar) ;
        _sbar.setProgress(Config.PowerValue);
        _sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Config.PowerValue=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getApplicationContext(), "功率设为："+Config.PowerValue,
                        Toast.LENGTH_SHORT).show();
            }
        });

        TextView _account=(TextView) findViewById(R.id.account);
        _account.setText("账号:"+Config.Account);
       Config.sm.openScan();
        Config.sm.setOutScanMode(0);
        showScanResult=(EditText) findViewById(R.id.editText);
        if(!Config.isLogin)
        {
            Config.isLogin=true;
           // openLogin(null);
        }
        Log.e("info","初始化完成");
    }
}
